from certificate_manager.models import *
from certificate_manager.serializers import DepartmentSerializer, UserSerializer, CertSerializer
from django.test import tag
from rest_framework import status
from rest_framework.renderers import JSONRenderer
from rest_framework.test import APITestCase, force_authenticate
import datetime

@tag('logined_account_tests')
class LoginedAccountTests(APITestCase):
	'''
	在这里我们要模拟用户的全部操作，包括注册、登陆、登出、更改用户相关信息
	'''
	def setUp(self):
		# 我们只需要把部门解决了，其他的就没问题
		Department.objects.create(name='test_department').save()
		self.cur_user = User.objects.create_user(uid='1111', email='1@qq.com', name='wyw', department=Department.objects.get(name='test_department'), password='123')
		User.objects.create_user(uid='1212', name='bad_guy', department=Department.objects.get(name='test_department'), password='123')
		self.assertEqual(self.client.login(uid='1111', password='123'), True)
	
	def test_update_my_info(self):
		# 请求链接
		url = f'/users/{self.cur_user.pk}/'		

		# 准备数据
		modify_user = UserSerializer(self.cur_user).data
		modify_user['email'] = 'xxx@qq.com'

		# 更新数据
		response = self.client.put(url, modify_user, format='json')
		
		# 刷新缓存
		del self.cur_user.email

		# 检查结果
		self.assertEqual(response.data['email'], 'xxx@qq.com')
		self.assertEqual(self.cur_user.email, 'xxx@qq.com')
		self.assertEqual(response.status_code, status.HTTP_200_OK)

	def test_update_other_info(self):
		# 请求链接
		url = f'/users/{self.cur_user.pk}/'		

		# 准备数据
		modify_user = UserSerializer(User.objects.get(name='bad_guy')).data
		modify_user['email'] = 'xxx@qq.com'
		modify_user['uid'] = 'xxx'

		# 更新数据
		response = self.client.put(url, modify_user, format='json')
		
		# 刷新缓存
		del self.cur_user.email

		# 检查结果
		self.assertEqual(response.status_code, status.HTTP_406_NOT_ACCEPTABLE)
		self.assertNotEqual(self.cur_user.email, 'xxx@qq.com')

	def test_update_password(self):
		# TODO: [待实现]更改密码应当使用不同的api
		pass

